package org.gwt.grin.rebind;

import java.io.IOException;

/**
 * A feature that's contained within another feature. This can be done by naming
 * some other feature, or by including it inline. Inline features are anonymous,
 * that is, they have no name; as such, they are always private.
 * <p>
 * During parsing, a SubFeature can be created for a named feature that doesn't
 * exist yet. In this case, the name is resolved to an actual feature the first
 * time getFeature() is called.
 * 
 * @author Bill Foote (http://jovial.com)
 * @author ggeorg
 */
public class SubFeature implements FeatureRef {

	private ShowBuilder builder = null;
	private String name = null; // null for an inline feature
	private SEFeature feature = null; // initially null for a named feature
	private Lexer lexer; // For error reporting

	SubFeature(SEFeature feature, Lexer lexer) {
		this.feature = feature;
		this.lexer = lexer;
	}

	SubFeature(ShowBuilder builder, String name, Lexer lexer) {
		this.builder = builder;
		this.name = name;
		this.lexer = lexer;
	}

	public SEFeature getFeature() throws IOException {
		if (feature == null) {
			feature = builder.getNamedFeature(name);
			if (feature == null) {
				lexer.reportError("Feature \"" + name + "\" not found!");
			}
		}
		return feature;
	}

}